package com.bawei.module_goods.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_goods.R
import com.bawei.module_goods.model.SearchGoodsItem

class SearchGoodsAdapter : ListAdapter<SearchGoodsItem, SearchGoodsAdapter.ViewHolder>(DIFF) {
    companion object {
        val DIFF = object : DiffUtil.ItemCallback<SearchGoodsItem>() {
            override fun areItemsTheSame(oldItem: SearchGoodsItem, newItem: SearchGoodsItem): Boolean = oldItem.goodsId == newItem.goodsId
            override fun areContentsTheSame(oldItem: SearchGoodsItem, newItem: SearchGoodsItem): Boolean = oldItem == newItem
        }
    }
    private var onItemClick: ((SearchGoodsItem) -> Unit)? = null
    fun setOnItemClickListener(listener: (SearchGoodsItem) -> Unit) {
        onItemClick = listener
    }
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvName: TextView = view.findViewById(R.id.tv_goods_name)
        val tvGoodsId: TextView = view.findViewById(R.id.tv_goods_id)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_search_goods, parent, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        holder.tvName.text = item.name
        holder.tvGoodsId.text = "ID: ${item.goodsId}"
        holder.itemView.setOnClickListener { onItemClick?.invoke(item) }
    }
    override fun getItemCount() = super.getItemCount()
} 